$(function () {

    layui.use('form', function () {
        var form = layui.form;

        form.on('submit(register)', function (data) {

            console.log("登录注册的数据:",data.field);

            $.ajax({
                // url: 'http://localhost:8080/back/user/insertUser',
                // url: 'http://localhost:11111/api/k/back/user/register',
                url: 'http://localhost:8080/back/userLogin/register',
                data: JSON.stringify(data.field),
                contentType:'application/json',

                type: 'post',
                dataType:'json',
                success: function (res) {
                    console.log("后端返回de数据", res)
                    if (res.data.message=="成功") {
                        //发异步，把数据提交给php
                        layer.alert("注册成功", {
                                icon: 6
                            },
                            function () {
                                location.href = "http://localhost:63342/kejifront-master/front_page/front/login1.html"
                            });
                    } else {
                        layer.alert("注册失败");
                    }
                }
            })
            return false;
        });

        form.verify({
            r_username:[/^[A-z]/,"名字必须以字母开头"],
            r_password:[/(.+){6,8}/,"密码长度6到12位"],
            r_repass: function (value) {
                if ($('#R_PASS').val() != $('#R_REPASS').val()) {
                    return '两次密码不一致';
                }
            }
        });


    });
})

function display(obj) {
    //当前是输入密码后面的图片
    if(obj.id == "pass_img"){
        //当前输入框类型是密码
        if($("#R_PASS").attr("type") == "password"){
            $("#R_PASS").attr("type","text")
            $(obj).attr("src","../img/open.png")
        }
        //当前输入框类型是文本
        else{
            $("#R_PASS").attr("type","password")
            $(obj).attr("src","../img/close.png")
        }

    }
    //当前是确认密码后面的图片
    else {
        //当前确认输入框类型是密码
        if($("#R_REPASS").attr("type") == "password"){
            $("#R_REPASS").attr("type","text")
            $(obj).attr("src","../img/open.png")
        }
        //当前确认输入框类型是文本
        else {
            $("#R_REPASS").attr("type","password")
            $(obj).attr("src","../img/close.png")
        }
    }
}

function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function clearCookie(name) {
    setCookie(name, "", -1);
}
function clearAllCookie() {
    var date=new Date();
    date.setTime(date.getTime()-10000);
    var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
    console.log("需要删除的cookie名字："+keys);
    if (keys) {
        for (var i =  keys.length; i--;)
            document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
    }
}
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}


